
*********************************
** Load correct visits effects:
	estimates use output/causal_visits
		global veffectp = _b[Np]
		global veffecte = _b[Ne]
*********************************



*****************************************
** Load relevant dataset & keep libraries for which all three exist

use "$datapath/intermediate/collect_pdouble.dta", clear
	
	gen id = 1 if raise=="Pe" 
	replace id = 2 if raise=="Pp"
	
	bys lno: gen Nlno = _N
	tab Nlno
	keep if Nlno==3
	distinct lno
	
*****************************************


*****************************************
** Keep libraries that solve to observed holding: 
	
	gen similar = (abs(Np-mNp)<.1 ) * (factor==10)
	egen SIMILAR = sum(similar), by(lno)
	tab SIMILAR if factor==10

	keep if SIMILAR==1
	
	
*****************************************
** Additional outcome variables: librarian utility and visits

** librarian utility: 
	gen lutility= (thetap*mQp + thetae*mQe)/1000000

** total circulation: 
	gen mQt = mQp + mQe

** library visits
	tempfile cf
	save `cf'
	
	use $datapath/clean/main_data.dta, clear 
		keep if year == 2018
		keep lno visits
		
		merge 1:m lno using `cf'
		keep if _merge==3
		drop _merge
		gen double mvisits = visits + ((mNp-Np)*($veffectp) + (mNe-Ne)*$veffecte)
*****************************************

************************
** Section 6.0 & Appendix A - shares of libraries solving to corners


	gen ap = mNp<1 | mNe<1 if raise=="Pp"
	egen AP = max(ap), by(lno)
	tab AP if raise=="Pp" & factor==20

	gen ae = mNp<1 | mNe<1 if raise=="Pe"
	egen AE = max(ae), by(lno)
	tab AE if raise=="Pe" & factor==10

	drop ae ap AE AP

** Drop libraries that solve to corners
	
	gen tag = mNp<1 | mNe<1
	egen TT = max(tag), by(lno)
	tab TT if raise=="Pe" & factor==10
	drop if TT==1
	

************************


***************************************************************
** Keep averages and create ratios:


	gen xc10 = CS if factor==10
	egen XC10 = mean (xc10), by(lno)	
	gen rCS = CS/XC10-1
	
	gsort lno factor raise
	gen rCS_ratio = rCS/rCS[_n-1] if raise=="Pp"
	
	collapse (mean) CS mNp mNe mQp mQe mQt mvisits (p50) rCS_ratio, by(id factor raise)

		foreach i in mNp mNe mQp mQe mQt CS mvisits {
			gen double x_`i' = `i' if factor==10
			egen double X_`i' = mean(x_`i')
			gen r_`i' = (`i'-X_`i')/X_`i'
		}

	keep if factor==20

		gen xr_CS = r_CS if raise=="Pe"
		egen Xr_CS = mean(xr_CS)
		gen r_CS_ratio = r_CS/Xr_CS if raise=="Pp"
		
		keep raise r_mN* r_mQp r_mQe r_CS* r_mvisits r_mQt rCS_ratio

*********************************************	

	
********************************************
** Create the table 
	
foreach v in r_mNp r_mNe r_mQp r_mQe r_mQt r_mvisits r_CS {
	replace `v' = 100*`v'
}
	
	label var r_CS "CS"
	label var r_mNe "ebook holdings"
	label var r_mNp "print holdings"
	label var r_mQe "ebook circulation"
	label var r_mQp "print circulation"
	label var r_mQt "total circulation"
	label var rCS_ratio "median $/Delta$ CS ratio"
	label var r_mvisits "visits"
	label var r_CS_ratio "mean $/Delta$ CS ratio"

	replace raise  = "raise $ P^e$" if raise == "Pe"
	replace raise  = "raise $ P^p$" if raise == "Pp"


eststo sumby: estpost tabstat r_mNp r_mNe r_mQp r_mQe r_mQt r_mvisits r_CS r_CS_ratio rCS_ratio, by(raise) stats(mean) columns(s) nototal 

esttab sumby, unstack replace label cells(`"mean(fmt(2)  label(" "))"')  gaps noobs nonumber 

esttab sumby using "$tablepath/table6_counterfactuals_main.tex", unstack replace label cells(`"mean(fmt(2)  label(" "))"') gaps booktabs noobs nonumber

********************************************

	
	
	
	
	
	
	
	
	
	
	
	